package com.ce.pub.process.action;


import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.exception.AppException;
import com.ce.pub.process.logic.ProcessDealService;
import com.opensymphony.xwork2.ActionContext;


public class ActivitiBaseAction extends  DataMaintainAction{
	private static final long serialVersionUID = -4393074864539616294L;

	private TaskService taskService;
	
	private ProcessDealService processDealService;

    private String processInstanceId;
	private String taskId;
	private String transition;
	private String definitionKey;
	/**
	 * 审批意见
	 */
	private String comment;



	/**
	 * 启动流程
	 */
	public String startProcess() {
		String[] vcIds = this.getParameterValues(WebConstants.SELECT_ITEMS_NAME);
		try {
			processDealService.startProcess(vcIds, po, definitionKey);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "提交申请成功!");
	}
	
	/**
	 * 跳到任务执行页
	 */
	public void toTask() throws Exception {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		Object model = taskService.getVariable(taskId,
				"model");
		ActionContext.getContext().getValueStack().push(model);

		//getButtonsForTransition(task);
		
		ActionContext.getContext().put("taskName", task.getName());
		//查询审批意见
		List<Map<String, Object>> list = processDealService.getComments(task.getProcessInstanceId());
		ActionContext.getContext().put("commentList", list);
		this.getRequest().getRequestDispatcher(task.getDescription()).forward(this.getRequest(),
				this.getResponse());
	
	}

	/**
	 * 执行任务
	 */
	public String performTask() {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		try {
			processDealService.doTask(transition, task, po, comment);
		} catch (AppException e) {
			return this.returnInfoForJS(false, "处理失败!");
		}
		return this.returnInfoForJS(true, "处理成功!");
	}

    public void readProcessPng()
            throws Exception {
        InputStream imageStream = processDealService.getProcessTaskImg(processInstanceId);

        // 输出资源内容到相应对象
        byte[] b = new byte[1024];
        int len;
        while ((len = imageStream.read(b, 0, 1024)) != -1) {
            this.getResponse().getOutputStream().write(b, 0, len);
        }
    }
    
    

	public String getProcessInstanceId() {
		return processInstanceId;
	}

	public void setProcessInstanceId(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public void setTransition(String transition) {
		this.transition = transition;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setDefinitionKey(String definitionKey) {
		this.definitionKey = definitionKey;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public ProcessDealService getProcessDealService() {
		return processDealService;
	}

	public void setProcessDealService(ProcessDealService processDealService) {
		this.processDealService = processDealService;
	}

	

}
